<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Hotel extends MY_Model {
    var $token = '1234567812341234123412345678';

    /**
     * Buat Alvin untuk halaman hotel.html
     * waktu autocomplete location
     *
     * panggil di controller dengan $this->hotel->get_location_data();
     */
    public function get_location_data() {
        $return         = array();

        $url            = 'http://api.hi-indonesia.com/hotel/json/city';
        $params         = array(
            'country_id' => 'ID',
            'token' => $this->token);

        $json_result    = json_decode($this->post($url, $params));

        if ($json_result != null && $json_result->status != null && $json_result->status->Code == "200") {
            $json_cities = $json_result->Cities[0];
            foreach ($json_cities as $value) {
                $return[$value->City->IdCity] = $value->City->Name . ' - ' . $value->City->IdCity;
            }
        }

        return $return;
    }
     public function get_promo_slider() {
        $return         = array();
        //$params dan $url di uncomment jika API sudah siap
        //isi dari URL masih imajinasi, jika sudah ada real dari alamat yang dimaksud, bisa langsung copas saja di situ.

        // $params         = array('country_id' => 'ID', 'token' => $this->token);
        // $url            = 'http://api.hi-indonesia.com/hotel/json/promo';
        $string = file_get_contents("slider.json");
        $json_result    = json_decode($string);

        //Jika API sudah siap 2 baris code diatas bisa di hapus kemudian uncomment baris yang dibawah ini

        //$json_result    = json_decode($this->post($url, $params));

        if ($json_result != null && $json_result->status != null && $json_result->status->Code == "200") {

            $json_sliders    = $json_result->Slider[0];
            foreach ($json_sliders as $value) {
                array_push($return, $value->Promo);
                // $return[$value->Promo->Title] = $value->Promo;
            }
        }

        return $return;
    }

    /**
     * Buat Alvin untuk halaman search-hotel.html
     * untuk mengambil data pencarian hotel pada SMAP
     *
     * panggil di controller dengan $this->hotel->get_searched_hotel();
     */
    public function get_searched_hotel($input_checkin, $input_checkout, $adult, $children, $city_code ) {
        $return['hotels']           = array();
        $return['SearchQueries']    = array();

        $checkin                    = date( 'd/m/Y', strtotime( $input_checkin));
        $checkout                   = date( 'd/m/Y', strtotime( $input_checkout));

        $datediff                   = strtotime($input_checkout) - strtotime($input_checkin);
        $duration                   = floor($datediff/(60*60*24));

        $url                        = 'http://api.hi-indonesia.com/hotel/json/search';
        $params = array(
            'adult'                 => intval($adult),
            'checkin_date'          => $checkin,
            'checkout_date'         => $checkout,
            'child'                 => intval($children),
            'city_code'             => $city_code,
            'cot'                   => 0,
            'duration'              => $duration,
            'hotel_name'            => '',
            'number_of_rooms'       => 1,
            'token'                 => $this->token);

        $json_result                = json_decode($this->post($url, $params));

        if ($json_result != null && $json_result->status != null && $json_result->status->Code == "200") {
            $json_hotels            = $json_result->Response->Hotels;

            foreach ($json_hotels as $value) {
                array_push($return['hotels'], $value->Hotel);
            }

            $return['SearchQueries'] = $json_result->Response->SearchQueries;
        }
        return $return;
    }

    /**
     * @author Victor Manu
     * Untuk Hotel details
     *
     * panggil di controller dengan $this->hotel->get_details_hotel();
     */
    public function get_details_hotel($hotel_code) {
        $return['hotels']           = array();
        $return['SearchQueries']    = array();

        $checkin                    = date( 'd/m/Y', strtotime( $_POST['check-in'] ));
        $checkout                   = date( 'd/m/Y', strtotime( $_POST['check-out']));

        $datediff                   = strtotime($_POST['check-out']) - strtotime($_POST['check-in']);
        $duration                   = floor($datediff/(60*60*24));

        $url                        = 'http://api.hi-indonesia.com/hotel/json/search';
        $params = array(
            'adult'                 => intval($_POST['adult']),
            'checkin_date'          => $checkin,
            'checkout_date'         => $checkout,
            'child'                 => intval($_POST['children']),
            'city_code'             => 'DPS',
            'cot'                   => 0,
            'duration'              => $duration,
            'hotel_name'            => '',
            'number_of_rooms'       => 1,
            'token'                 => $this->token);

        $json_result                = json_decode($this->post($url, $params));

        if ($json_result != null && $json_result->status != null && $json_result->status->Code == "200") {
            $json_hotels            = $json_result->Response->Hotels;

            foreach ($json_hotels as $value) {
                array_push($return, $value->Hotel);
            }

            $return['SearchQueries'] = $json_result->Response->SearchQueries;
        }
        return $return;
    }

    /**
     * @author Victor Manu
     * Untuk booking, get PNR Hotel
     *
     * panggil di controller dengan $this->hotel->get_pnr_hotel();
     */
    public function get_pnr_hotel($htid) {
        $return = '';

        $url = 'http://api.hi-indonesia.com/hotel/json/pnr';
        $params = array(
            'htid'  => $htid,
            'token' => $this->token);

        $json_result = json_decode($this->post($url, $params));

        if ($json_result != null && $json_result->status != null && $json_result->status->Code == "200") {
            $return = $json_result->Response;
        }

        return $return;
    }

    public function get_hotel_detail() {
        $return         = array();

        $params         = array('hotel_code' => 'ANE:10', 'token' => '1234567812341234123412345678');
        $url            = 'http://api.hi-indonesia.com/hotel/json/details';

        $json_result    = json_decode($this->post($url, $params));

        if ($json_result != null && $json_result->status != null && $json_result->status->Code == "200") {
            $json_hotels_detail    = $json_result->DetailHotel->HotelCode;

            foreach ($json_hotels_detail as $value) {
                array_push($return, $value->HotelCode);
            }
        }

        return $return;
    }

    public function booking($htid, $input) {
        $return = array();

        $contact = array(
            'email'             => $input['email-ci'],
            'first_name'        => $input['first-name-ci'],
            'last_name'         => $input['last-name-ci'],
            'telepon_code'      => intval($input['country-code']),
            'telepon_number'    => intval(str_replace($input['country-code'], "", $input['phone-primary'])),
            'title'             => $input['title-ci']);

        $adults = array();

        for ($i = 0; $i < count($input['title-adult']); $i++) {
            array_push($adults, array(
                'first_name'    => $input['first-name-adult'][$i],
                'last_name'     => $input['last-name-adult'][$i],
                'room_number'   => 1,
                'title'         => $input['title-adult'][$i]));
        }

        $children = array();

        if (isset($input['age-child'])) {
            for ($i = 0; $i < count($input['age-child']); $i++) {
                array_push($children, array(
                    'age'           => $input['age-child'][$i],
                    'first_name'    => $input['first-name-child'][$i],
                    'last_name'     => $input['last-name-child'][$i],
                    'room_number'   => 1,
                    'title'         => ""));
            }
        }

        $prices = array(
            'current_price'     => intval($input['total-pax']),
            'profit'            => intval($input['total-pax']),
            'published_price'   => intval($input['total-pax']),
            'tax_price'         => intval($input['total-pax']));

        $params = array(
            'Contact'           => $contact,
            'adult'             => $adults,
            'child'             => $children,
            'htid'              => $htid,
            'number_room'       => intval($input['total-room']),
            'prices'            => $prices,
            'remark'            => $input['remark'],
            'token'             => $this->token);

        $url            = 'http://api.hi-indonesia.com/booking/json/hotel';
        $json_result    = json_decode($this->post($url, $params));

        if ($json_result != null && $json_result->status != null && $json_result->status->Code == "200") {
           $return = $json_result->results;
        }

        return $return;
    }

    public function get_hotel_attribut($hotel_code) {
        $return         = '';

        $url            = 'http://api.hi-indonesia.com/hotel/json/details';
        $params         = array(
            'hotel_code' => $hotel_code,
            'token' => $this->token);

        $json_result    = json_decode($this->post($url, $params));

        if ($json_result != null && $json_result->status != null && $json_result->status->Code == "200") {
            $return= $json_result->DetailHotel;
        }

        return $return;
    }

}
